<?php

class Ecard_Model_Image extends Core_Model_Item_Abstract {
   
    public function getCategoryName(){
        $category = Engine_Api::_()->getItem('ecard_category',$this->category_id);
        return $category->category_name;       
    }    
    
    public function getPhotoUrl($type = null)
    {
        if( empty($this->photo_id) ) {
          $photoTable = Engine_Api::_()->getItemTable('ecard_image');
          $photoInfo = $photoTable->select()
              ->from($photoTable, array('photo_id', 'photo_id'))
              ->where('image_id = ?', $this->image_id)
              ->limit(1)
              ->query()
              ->fetch();
          if( !empty($photoInfo) ) {
            $this->photo_id = $photo_id = $photoInfo['photo_id'];
            $this->save();
            $file_id = $photoInfo['photo_id'];
          } else {
            return;
          }
        } else {
          $photoTable = Engine_Api::_()->getItemTable('ecard_image');
          $file_id = $photoTable->select()
              ->from($photoTable, 'photo_id')
              ->where('image_id = ?', $this->image_id)
              ->query()
              ->fetchColumn();
        }
        
        if( !$file_id ) {
          return;
        }
        
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($file_id, $type);
        if( !$file ) {
          return;
        }
        
        //return $file->map();
        return $file->storage_path;
    }
    
}